package it.tcm.TeacherClassMate.facade;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;


import it.tcm.TeacherClassMate.beans.UserBean;
import it.tcm.TeacherClassMate.dao.UserDao;
import it.tcm.TeacherClassMate.persistent.HibernateUtil;


public class UserManageBase {

	 	private Logger log = Logger.getLogger(this.getClass().getName());   
	    private UserDao userDao = new UserDao();
	  
	    public UserBean getUserById(Integer id) {
		    Session session = null;
		    UserBean utente = null;
			try {
				session = HibernateUtil.createNewSessionAndTransaction();
				userDao.setSession(session);
			    utente = userDao.findById(id, false);
			    HibernateUtil.commitTransactionAndCloseSession(session);
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    log.error("Error in reading user "  + id.toString(), e);
			} finally {
			}
			return utente;
	    }
	    
	    public List<UserBean> getUserBeanByEmail(String email) {
	    	Session session = null;
	    	List<UserBean> userList = null;
			try {
				 session = HibernateUtil.createNewSessionAndTransaction();
				 ArrayList<Order> order = new ArrayList<Order>();
				 order.add(Order.asc("email"));		 				    
				 ArrayList<Criterion> criterion = new ArrayList<Criterion>();
				 session.createCriteria(UserBean.class);               
				 Criterion c = Restrictions.eq("email", email).ignoreCase();
				 criterion.add(c);				
				 userDao.setSession(session);
				 userList = userDao.findByCriteria(order, (Criterion[]) criterion.toArray(new Criterion[criterion.size()]));				 
			     HibernateUtil.commitTransactionAndCloseSession(session);  
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    log.error("Error in reading users by email", e);
			}
			return userList;
	    }	
	    
	    public UserBean getUserBeanByUsernameAndPassword(String username,String password) {
	    	Session session = null;
	    	UserBean user = null;
			try {
				 session = HibernateUtil.createNewSessionAndTransaction();
				 ArrayList<Order> order = new ArrayList<Order>();
				 order.add(Order.asc("username"));		 				    
				 ArrayList<Criterion> criterion = new ArrayList<Criterion>();
				 session.createCriteria(UserBean.class);               
				 Criterion c = Expression.eq("username", username);
				 criterion.add(c);	
				 Criterion c1 = Expression.eq("password", password);
				 criterion.add(c1);
				 userDao.setSession(session);
				 user = userDao.UniqueResultByCriteria(order, (Criterion[]) criterion.toArray(new Criterion[criterion.size()]));				 
			     HibernateUtil.commitTransactionAndCloseSession(session);  
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    log.error("Error in reading user by username and password", e);
			}
			return user;
	    }	
	    
	    public UserBean getUserBeanByUsername(String username) {
	    	Session session = null;
	    	UserBean user = null;
			try {
				 session = HibernateUtil.createNewSessionAndTransaction();
				 ArrayList<Order> order = new ArrayList<Order>();
				 order.add(Order.asc("username"));		 				    
				 ArrayList<Criterion> criterion = new ArrayList<Criterion>();
				 session.createCriteria(UserBean.class);               
				 Criterion c = Expression.eq("username", username);
				 criterion.add(c);	
				 userDao.setSession(session);
				 user= userDao.UniqueResultByCriteria(order, (Criterion[]) criterion.toArray(new Criterion[criterion.size()]));				 
			     HibernateUtil.commitTransactionAndCloseSession(session);  
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    log.error("Error in reading user by username", e);
			}
			return user;
	    }	
	    
	    public List<UserBean> getUserBeanByCode(String code) {
	    	Session session = null;
	    	List<UserBean> userList = null;
			try {
				 session = HibernateUtil.createNewSessionAndTransaction();
				 ArrayList<Order> order = new ArrayList<Order>();
				 order.add(Order.asc("code"));		 				    
				 ArrayList<Criterion> criterion = new ArrayList<Criterion>();
				 session.createCriteria(UserBean.class);               
				 Criterion critIdElementoArc = Restrictions.eq("code", code).ignoreCase();
				 criterion.add(critIdElementoArc);				
				 userDao.setSession(session);
				 userList = userDao.findByCriteria(order, (Criterion[]) criterion.toArray(new Criterion[criterion.size()]));				 
			     HibernateUtil.commitTransactionAndCloseSession(session);  
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    log.error("Error in reading users by code", e);
			}
			return userList;
	    }	
	    
	    public boolean save(UserBean user)
	    {
	  	  boolean result = false;	  
	  	  Session session = null;
	  	  try 
	  	  {
	  		session = HibernateUtil.createNewSessionAndTransaction();
	  		userDao.setSession(session);
	  		userDao.attachDirty(user);
	  		HibernateUtil.commitTransactionAndCloseSession(session);
	  		result = true;
	  	  } 
	  	  catch (Exception e) 
	  	  {
	  		  log.error("Errore in save user ", e);
	  		  HibernateUtil.rollbackTransaction(session);
	  	  } 	   	  
	  	  return result;
	    }
	    
	    public List<UserBean> getAll() {
		    Session session = null;
		    List<UserBean> uList = null;
			try {
				session = HibernateUtil.createNewSessionAndTransaction();
				userDao.setSession(session);
				uList = userDao.findAll();
			    HibernateUtil.commitTransactionAndCloseSession(session);
			} catch (Exception e) {
				HibernateUtil.rollbackTransaction(session);
			    log.error("Errore in reading all users ", e);
			} finally {
			}
			return uList;
	    }
	   

}
